<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UserControllerSiab extends MX_Controller {

    private $_template = "siab_unp";

    function __construct()
	{
		parent::__construct();
        $this->access_library_siab->is_logged_in();
    }

    function checkLoginData()
    {
        $this->load->model("user_model");

        $postUsername = $this->security->xss_clean($this->input->post('username'));
        $postPassword = $this->security->xss_clean($this->input->post('password'));

        $dataLogin = $this->user_model->getDataLogin($postUsername);

        if($dataLogin->num_rows()>0){

          $dataLogin = $dataLogin->row();
          if (md5($dataLogin->PASSWORD)==md5($postPassword)) {

              $viewKontrolData = $this->user_model->getViewKontrolData($dataLogin->PRODI);

              $sessionData = array(
                            'siab_user_username' => $postUsername,
                            'siab_user_kode_prodi' => $dataLogin->PRODI,
                            'siab_user_nama_prodi' => $dataLogin->NAMA_PRODI,
                            'siab_user_level' => $dataLogin->LEVEL,
                            'siab_system_semester' => $viewKontrolData->KODE_SEMESTER,
                            'siab_system_tahun_ajaran' => $viewKontrolData->KODE_TAHUN,
                            'siab_system_status' => $viewKontrolData->KODE_STATUS
                            );
               $this->session->set_userdata($sessionData);
               return TRUE;
           }
           else
               return FALSE;
         }
         else
           return FALSE;
    }

	function login()
	{
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('chk_pwd', '', 'callback_checkLoginData');
        $this->form_validation->set_message('required', 'Error: %s is Mandatory');
        $this->form_validation->set_message('checkLoginData', 'Error: Username or Password is incorrect');

        if ($this->form_validation->run($this) == FALSE)
        {
          $this->load->view('login');
        }
        else
          redirect('siab', 'refresh');
	}

    function logout() {
        $data = array(
                 'siab_user_username' => 0,
                 'siab_user_kode_prodi' => 0,
                 'siab_user_nama_prodi' => 0,
                 'siab_user_level' => 0,
                 'siab_system_semester' => 0,
                 'siab_system_tahun_ajaran' => 0,
                 'siab_system_status' => 0
				 );

        $this->session->sess_destroy();
        $this->session->unset_userdata($data);
		redirect('siab/login.html', 'refresh');
	}
    function viewWelcome()
	{
        $data['page'] = 'siab_home';
        $this->load->view($this->_template,$data);
	}
}

